import { Component, OnInit, Input } from '@angular/core';
import { RatingOverviewModel } from '../models/ratingOverview.model';
import { UtilsService } from '../../../../../shared/services/utils.service';
import { LrtmService } from '../../../../../framework/lrtm/lrtm.service';
import { AppComponent } from '../../../../../app.component';
import { ResearchState } from '../../../research.service';

@Component({
  selector: 'app-research-overview',
  templateUrl: './research-overview.component.html',
  styleUrls: ['./research-overview.component.scss']
})
export class ResearchOverviewComponent implements OnInit {
  public researchState: typeof ResearchState = ResearchState;

  @Input() ratingModel: RatingOverviewModel;
  @Input() state: ResearchState;
  @Input() currentRatingFormat = '1.1';
  @Input() route: any[];
  @Input() hasAccess = false;
  @Input() company;
  @Input() hasDownloadIcon : boolean = true;

  protected iconsBasePath = '/assets/icons-svg';
  protected researchIcons = {
    available: 'icon-research-available.svg',
    unavailable: 'icon-research-not-available.svg',
    inProgress: 'icon-research-inprogress.svg',
  };

  constructor(
    private rootComponent: AppComponent,
    private lrtmService: LrtmService
  ) { }

  ngOnInit() {
  }

  get availableIcon() { return this.getIconDisplay(this.researchIcons.available); }
  get unavailableIcon() { return this.getIconDisplay(this.researchIcons.unavailable); }
  get inProgressIcon() { return this.getIconDisplay(this.researchIcons.inProgress); }
  get ratingIcon() { return this.getIconDisplay(this.ratingModel.ratingIcon); }

  //get researchAvailable() { return (this.ratingModel.currentRating || this.ratingModel.ratingAssessment); }

  private getIconDisplay(iconPath: string) {
    return `${this.iconsBasePath}/${iconPath}`;
  }

  public downloadReport() {
    // set report name
    const reportName = UtilsService.getCompanyReportName(this.company, this.ratingModel.downloadData.reportName, false);

    // add isSummary to reportData
    if (this.ratingModel.downloadData.reportData instanceof Object) {
      this.ratingModel.downloadData.reportData.isSummaryReport = false;
    }

    // run task
    this.lrtmService
      .runTask(this.rootComponent, reportName, this.ratingModel.downloadData.reportType, this.ratingModel.downloadData.reportData, true)
      .subscribe(report => { });
  }

}
